const app = getApp()
Page({
  data: {
    Xm: '',
    Zh: '',
    Mm: '',
    Sf: ''
  },
  onLoad: function() {
    wx.cloud.database({
      env: 'gtss-2b1812'
    }).collection('Config').get().then(res => {
      app.globalData.stuKtmax = res.data[0].stuKtmax
      app.globalData.teaStumax = res.data[0].teaStumax
    })
  },
  inpXm: function(e) {
    this.setData({
      Xm: e.detail.value
    })
  },
  inpZh: function(e) {
    this.setData({
      Zh: e.detail.value
    })
  },
  inpMm: function(e) {
    this.setData({
      Mm: e.detail.value
    })
  },
  radioChange: function(e) {
    this.setData({
      Sf: e.detail.value
    })
  },
  funLogin: function() {
    if (this.data.Xm == '') {
      wx.showToast({
        title: '姓名不能为空',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (this.data.Zh == '') {
      wx.showToast({
        title: '号码不能为空',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (this.data.Mm == '') {
      wx.showToast({
        title: '密码不能为空',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (this.data.Sf == '') {
      wx.showToast({
        title: '请选择用户类型',
        icon: 'none',
        duration: 2000
      })
      return
    }
    wx.cloud.callFunction({
      name: 'login',
      data: {
        Xm: this.data.Xm,
        Zh: this.data.Zh,
        Mm: this.data.Mm,
        Sf: this.data.Sf
      }
    }).then(res => {
      if (res.result.result == 1) {
        app.globalData.userId = this.data.Zh
        app.globalData.userName = this.data.Xm
        app.globalData.userDept = res.result.dept
        app.globalData.userState = res.result.userState
        app.globalData.userType = 1
        wx.switchTab({
          url: '../stuNews/stuNews'
        })
      } else if (res.result.result == 2) {
        app.globalData.userId = this.data.Zh
        app.globalData.userName = this.data.Xm
        app.globalData.userDept = res.result.dept
        app.globalData.userState = res.result.userState
        app.globalData.userType = 2
        wx.redirectTo({
          url: '../teaSy/teaSy'
        })
      } else if (res.result.result == 3) {
        app.globalData.userId = this.data.Zh
        app.globalData.userName = this.data.Xm
        app.globalData.userDept = res.result.dept
        app.globalData.userType = 3
        wx.redirectTo({
          url: '../admSy/admSy'
        })
      } else {
        wx.showToast({
          title: '登录信息有误',
          icon: 'none',
          duration: 2000
        })
      }
    })
  }
})